<script>
	function formatMoney(num) {
	    var p = num.toFixed(0).split(".");
	    return p[0].split("").reverse().reduce(function(acc, num, i, orig) {
	        return  num + (i && !(i % 3) ? "." : "") + acc;
	    }, "");
	}
	function chonKhoahoc(itemSelect) {
		$('body').append('<div class="loading_page"></div>');
		$('input[type=checkbox]', '#tblKhuyenmai').prop('checked', false);
		
		id_khoahoc 	= $(itemSelect).val();
		var urlLophoc	= '<?php echo $this->baseUrl('khachhang/ajax/ajax-lophoc/id_khoahoc/');?>' + id_khoahoc;
		$('#lophoc').load(urlLophoc, function(){ $('.loading_page').remove();});

		var urlKhoahoc	= '<?php echo $this->baseUrl('khachhang/ajax/ajax-khoahoc/id_khoahoc/');?>' + id_khoahoc;
		$.getJSON(urlKhoahoc, {'id_khoahoc' : id_khoahoc}, function(data) {
			if (data) {
				$('#spanDongia').text(formatMoney(parseInt(data.price)));
				$('#dongia').val(data.price);

				$('#spanThanhtien').text(formatMoney(parseInt(data.price)));
				$('#thanhtien').val(data.price);

				$('#spanDangky').text(0);
				$('#spanTongDangky').text(data.soluongnguoitoida);

				$('#canhbao').text('');

				thanhTien();
			} else {
				$('#spanDongia').text(0);
				$('#dongia').val(0);
				
				$('#spanThanhtien').text(0);
				$('#thanhtien').val(0);

				$('#spanDangky').text(0);
				$('#spanTongDangky').text(0);

				thanhTien();
			}
		});

		
	}

	function chonLophoc(itemSelect) {
		id_lophoc 	= $(itemSelect).val();
		var urlLophoc	= '<?php echo $this->baseUrl('khachhang/ajax/json-lophoc');?>';
		$.getJSON(urlLophoc, {'id_lophoc' : id_lophoc}, function(data) {
			if (data) {
				$('#spanDangky').text(data.total_hopdong);
				if(data.total_hopdong >= $('#spanTongDangky').text()){
					$('#canhbao').text('(Full)');
				}
			} else {
				$('#spanDangky').text(0);
			}
		});
	}

	function thanhTien(){
		var dongia = $('#dongia').val();
		if(dongia == '0'){
			$('#spanThanhtien').text('0');
			$('#thanhtien').val(0);
			$('#tongtienphaidong').text('0');
			$('#sotienconno').text('0');
			$('#sotienphaitralai').text('0');
		} else {
			var tongItem = 0;
			var tongPhantram = 0;
			var tongGiagiam = 0;
			$('input[type=checkbox]#itemCheck', '#tblKhuyenmai').each(function(index){
				if($(this).is(':checked')){
					tongItem++;
					var idItem = $(this).val();
					tongPhantram = tongPhantram + parseInt($('#tdPhantram_' + idItem).text());
					tongGiagiam = tongGiagiam + parseInt($('#tdGiagiam_' + idItem).text().replace(/\./gi, ''));
				}
			});
			var giaPhantram = parseInt(tongPhantram) / 100 * parseInt(dongia);
			var giaGiam = tongGiagiam;
			dongia = parseInt(dongia) - giaPhantram - giaGiam;
			$('#spanThanhtien').text(formatMoney(parseInt(dongia)));
			$('#tongtienphaidong').text(formatMoney(parseInt(dongia)));
			$('#thanhtien').val(dongia);

			var sotiendadong = parseInt($('#sotiendadong').text().replace(/\./gi, ''));
			var sotienconno = 0;
			if(sotiendadong < dongia){
				$('#sotienphaitralai').text('0');
				sotienconno = dongia - sotiendadong;
				$('#sotienconno').text(formatMoney(parseInt(sotienconno)));
			} else {
				$('#sotienconno').text('0');
				$('#sotienphaitralai').text(formatMoney(parseInt(sotiendadong - dongia)));
			}

			//Input hiden
			$('#hd_thanhtien').val(dongia);
			$('#hd_sotiendadong').val(sotiendadong);
			$('#hd_sotienno').val(sotienconno);
		}
	}
	
<?php
	if(!empty($this->Khoahoc)) {
?>
	$('#spanDongia').text(formatMoney(parseInt(<?php echo $this->Khoahoc['price'];?>)));
	$('#dongia').val(<?php echo $this->Khoahoc['price'];?>);

	$('#spanThanhtien').text(formatMoney(parseInt(<?php echo $this->Khoahoc['price'];?>)));
	$('#thanhtien').val(<?php echo $this->Khoahoc['price'];?>);

	$('#spanTongDangky').text(<?php echo $this->Khoahoc['soluongnguoitoida'];?>);

	var soluongnguoitoida = <?php echo $this->Khoahoc['soluongnguoitoida'];?>;
<?php
	} 
?>
<?php
	if(!empty($this->Lophoc)) {
?>
	var total_hopdong = <?php echo $this->Lophoc['total_hopdong'];?>;
	$('#spanDangky').text(<?php echo $this->Lophoc['total_hopdong'];?>);
	if(total_hopdong >= soluongnguoitoida){
		$('#canhbao').text('(Full)');
	}
<?php
	} 
?>	

	$(document).ready(function(){
		thanhTien();
		$('input[type=checkbox]#itemCheck', '#tblKhuyenmai').each(function(index){
			$(this).click(function(){
				thanhTien();
			});
		});
	});
</script>
<?php
	$khoahoc = 0;
	if (!empty($this->arrParam['id_khoahoc'])) {
		$khoahoc = $this->arrParam['id_khoahoc'];
	}
	
	$lophoc = 0;
	if (!empty($this->arrParam['id_lophoc'])) {
		$lophoc = $this->arrParam['id_lophoc'];
	}
	
	$khuyenmai = 0;
	if (!empty($this->arrParam['id_khuyenmai'])) {
		$khuyenmai = $this->arrParam['id_khuyenmai'];
	}
	
	$id_khoahoc  	= $this->formSelect('id_khoahoc', $this->arrParam['id_khoahoc'], array('style'=>'width:100%;', 'onChange' => 'chonKhoahoc(this)'), $this->slbKhoahoc) . '<div class="view_error" id="view_id_khoahoc"></div>';
	$id_lophoc  	= $this->formSelect('id_lophoc', $this->arrParam['id_lophoc'], array('style'=>'width:100%;', 'onChange' => 'chonLophoc(this)'), $this->slbLophoc);
	$id_khuyenmai  	= $this->formSelect('id_khuyenmai', $this->arrParam['id_khuyenmai'], array('style'=>'width:100%;'), $this->slbKhuyenmai);
?>
<table class="tbl">
	<tr>
		<td class="label">Chọn khóa học</td>
		<td><?php echo $id_khoahoc; ?></td>
		
		<td class="label dam">Đơn giá:</td>
		<td style="padding-top: 9px;">
			<div id="divDongia">
				<span id="spanDongia">0</span> (VNĐ)
				<input type="hidden" name="dongia" id="dongia" readonly="readonly" value="0"/>
			</div>
		</td>
	</tr>
	<tr>
		<td class="label">Chọn lớp học</td>
		<td><div id="lophoc"><?php echo $id_lophoc; ?></div></td>
		
		<td class="label dam">Số lượng đăng ký:</td>
		<td style="padding-top: 9px;">
			<div id="soluongdangky">
				<span id="spanDangky">0</span>/<span id="spanTongDangky">0</span> (KH) <span class="red" id="canhbao"></span>
			</div>
		</td>
	</tr>
	
	<tr>
		<td class="label">Khuyến mại</td>
		<td colspan="3">
			<div style="border: 1px solid #efefef; width: 772px; max-height: 120px; overflow: auto; padding: 1px;">
				<table class="tblList" id="tblKhuyenmai">
					<tr>
						<th width="20"></th>
						<th>Tên chương trình</th>
						<th width="80">Giảm theo %</th>
						<th width="80">Trừ trực tiếp</th>
						<th width="80">Số lượng còn</th>
					</tr>
					<?php
					$id_getKhuyenmai = explode(',', $this->arrParam['id_khuyenmai']);
					foreach ($this->Khuyenmai as $key => $val){
						$id 			= $val['id'];
						$name 			= $val['name'];
						$phantram		= $val['phantram'];
						$giagiam		= $val['giagiam'];
						$soluong		= $val['soluong'];
						$ngaybatdau		= $val['ngaybatdau'];
						$ngayketthuc	= $val['ngayketthuc'];
						if($val['soluong'] == 0) {
							$soluong = '&infin;';
						}
						
						$arrAtr = array(
									'id' => 'itemCheck'
								);
						if(in_array($val['id'], $id_getKhuyenmai)) {
							$arrAtr['checked'] = 'checked';
						}
						$cid 			= $this->formCheckbox('id_khuyenmai[]',$val['id'], $arrAtr);
						$classRow = 'row1';
						if($key % 2 == 0)
							$classRow = '';
					?>
					<tr class="<?php echo $classRow;?>">
						<td align="center"><?php echo $cid;?></td>
						<td align="left" style="color: #0051A4;">
							<a href="javascript:void(0)" title="<?php echo $ngaybatdau . ' - ' . $ngayketthuc;?>"><?php echo $name;?></a>
						</td>
						<td align="center" id="tdPhantram_<?php echo $val['id'];?>"><?php echo $phantram;?></td>
						<td align="right" id="tdGiagiam_<?php echo $val['id'];?>" class="tdGiagiam"><?php echo $giagiam;?></td>
						<td align="center"><?php echo $soluong;?></td>
					</tr>
					<?php
					} 
					?>
				</table>
			</div>
		</td>
	</tr>
	<tr>
		<td class="label"></td>
		<td></td>
		
		<td class="label dam">Tổng thành tiền:</td>
		<td style="padding-top: 9px;">
			<div id="divThanhtien">
				<span id="spanThanhtien" class="red">0</span> (VNĐ)
				<input type="hidden" name="thanhtien" id="thanhtien" value="0"/>
			</div>
		</td>
	</tr>
</table>



